package com.qf.factory;

import org.springframework.beans.factory.BeanFactory;

import java.util.Properties;

public class MyBeanFactory {

    private  Properties properties = new Properties();

    /**
     * 无参构造
     */
    public MyBeanFactory() {
    }
    /**
     *  有参构造
     * @param config
     * @throws Exception
     */
    public MyBeanFactory(String config) throws Exception{
        //通过输入流根据配置文件的名字和路径, 读取配置文件到properties对象中
        properties.load(BeanFactory.class.getResourceAsStream(config));
    }

    /**
     * 获取JavaBean 对象
     * @param beanName
     * @return
     * @throws Exception
     */
    public Object getBean (String beanName) throws  Exception{
        //获取类的路径->>>>>>实现类的全限定名
        String classPath = properties.getProperty(beanName);
        System.out.println(classPath);
        //判断路径是否为空   不为空的话用反射获取类对象
        if (classPath !=null){
            Class clazz = Class.forName(classPath);
            return clazz.newInstance();
        }

        return null;
    }
}
